﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProductManager.bl;
using System.Linq;
using System.Collections;

namespace ProductManager.gui
{
    public partial class GUICategory : DevExpress.XtraEditors.XtraForm
    {
        private Category category = null;
        private IEnumerable<Category> listCategory = null;
        private ProductMDDataContext ctx = null;
        private BLCategory blCategory = null;
        private int status = 0;
        private string sname = "";

        private int currentpage = 0;
        private int totalpage = 0;
        private int pageSize = 10;

        public GUICategory()
        {
            InitializeComponent();
            RefreshData();
        }

        private void ResetText()
        {
            txtCategoryID.Text = "";
            txtCategoryName.Text = "";
            txtDescription.Text = "";
            rbtnActive.SelectedIndex = 0;
        }

        private void RefreshData()
        {
            try
            {
                status = 0;
                ctx = new ProductMDDataContext();
                var list = ctx.Categories;
                listCategory = list.AsEnumerable();

                LoadData();
            }
            catch (Exception ex)
            {

            }
        }

        private void LoadData()
        {
            try
            {
                CreatePage();
                ResetText();
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
                btnAdd.Enabled = false;

            }
            catch (Exception ex)
            {

            }
        }

        private void LoadDataCurrent(IEnumerable<Category> data)
        {
            grvCategory.Columns.Clear();
            grvCategory.AutoGenerateColumns = false;
            grvCategory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            DataGridViewCell cell = new DataGridViewTextBoxCell();

            DataGridViewColumn columnID = new DataGridViewColumn();
            columnID.HeaderText = "ID";
            columnID.Name = "ID";
            columnID.Visible = true;
            columnID.ReadOnly = true;
            columnID.CellTemplate = cell;
            columnID.DataPropertyName = "CategoryID";

            DataGridViewColumn columnCategoryName = new DataGridViewColumn();
            columnCategoryName.HeaderText = "Category Name";
            columnCategoryName.Name = "Catefory Name";
            columnCategoryName.Visible = true;
            columnCategoryName.ReadOnly = true;
            columnCategoryName.CellTemplate = cell;
            columnCategoryName.DataPropertyName = "CategoryName";

            DataGridViewColumn columnDescription = new DataGridViewColumn();
            columnDescription.HeaderText = "Description";
            columnDescription.Name = "Description";
            columnDescription.Visible = true;
            columnDescription.ReadOnly = true;
            columnDescription.CellTemplate = cell;
            columnDescription.DataPropertyName = "Description";


            DataGridViewCheckBoxColumn columnActive = new DataGridViewCheckBoxColumn();
            columnActive.HeaderText = "Active";
            columnActive.Name = "Active";
            columnActive.Visible = true;
            columnActive.ReadOnly = true;
            cell = new DataGridViewCheckBoxCell();
            columnActive.CellTemplate = cell;
            columnActive.DataPropertyName = "Status";

            grvCategory.Columns.Add(columnID);
            grvCategory.Columns.Add(columnCategoryName);
            grvCategory.Columns.Add(columnDescription);
            grvCategory.Columns.Add(columnActive);
            grvCategory.DataSource = data;
        }

        //==============================================//
        //        Pagination with sql and linq          //
        //==============================================//


        private void CreatePage()
        {
            currentpage = 0;

            if (listCategory.Count() % pageSize == 0)
            {
                totalpage = listCategory.Count() / pageSize;
            }
            else
            {
                totalpage = listCategory.Count() / pageSize + 1;
            }

            lblTotalPage.Caption = "Total page: " + totalpage.ToString();
            lblTotalCategory.Caption = "Total Category: " + listCategory.Count().ToString();


            if (totalpage != 0)
            {
                GoToPage(0);
            }
            else
            {
                grvCategory.Rows.Clear();
                barbtnFirst.Enabled = false;
                barbtnLast.Enabled = false;
                barbtnNext.Enabled = false;
                barbtnPrevious.Enabled = false;
                barbtnPreviousPage.Enabled = false;
                barbtnNextPage.Enabled = false;
            }

        }

        private void GoToPage(int page)
        {
            currentpage = page;
            if (status == 0)
            {
                LoadDataCurrent(ctx.Categories.Skip(currentpage * pageSize).Take(pageSize));
            }
            else
            {
                var list1 = ctx.Categories.Where(c => c.CategoryName.Contains(sname)).Skip(currentpage * pageSize).Take(pageSize);
                LoadDataCurrent(list1);
            }


            if (currentpage == 0)
            {
                barbtnFirst.Enabled = false;
                barbtnPrevious.Enabled = false;
            }
            else
            {
                barbtnFirst.Enabled = true;
                barbtnPrevious.Enabled = true;
            }

            if (currentpage == totalpage - 1)
            {
                barbtnNext.Enabled = false;
                barbtnLast.Enabled = false;
            }
            else
            {
                barbtnNext.Enabled = true;
                barbtnLast.Enabled = true;
            }

            if ((currentpage - 5) < 0)
            {
                barbtnPreviousPage.Enabled = false;
            }
            else
            {
                barbtnPreviousPage.Enabled = true;
            }

            if ((currentpage + 5) > totalpage - 1)
            {
                barbtnNextPage.Enabled = false;
            }
            else
            {
                barbtnNextPage.Enabled = true;
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            ResetText();
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            btnAdd.Enabled = true;
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (blCategory.ValidateAdd(txtCategoryName.Text, txtDescription.Text))
            {
                category = new Category();
                category.CategoryName = txtCategoryName.Text;
                category.Description = txtDescription.Text;
                if (rbtnActive.SelectedIndex == 0)
                {
                    category.Status = true;
                }
                else
                {
                    category.Status = false;
                }

                ctx.add_Category(category.CategoryName, category.Description, category.Status);
                ctx.SubmitChanges();
                MessageBox.Show(this, "Add successful", "Add Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                RefreshData();

                btnAdd.Enabled = false;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(this, "Do you want to update " + category.CategoryName + " ?", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                if (category != null)
                {
                    if (blCategory.ValidateAdd(txtCategoryID.Text, txtDescription.Text))
                    {
                        category.CategoryName = txtCategoryName.Text;
                        category.Description = txtDescription.Text;
                        if (rbtnActive.SelectedIndex == 0)
                        {
                            category.Status = true;
                        }
                        else
                        {
                            category.Status = false;
                        }
                        ctx.SubmitChanges();
                    }
                }
                ctx.SubmitChanges();
                MessageBox.Show(this, "Update successful", "Update Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                RefreshData();
            }
        }

        private void GUICategory_Load(object sender, EventArgs e)
        {
            ctx = new ProductMDDataContext();
            blCategory = new BLCategory();
            txtCategoryID.Properties.ReadOnly = true;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (blCategory.ValidateSearchByName(txtSearchName.Text))
            {
                if (ctx != null)
                {
                    status = 1;
                    sname = txtSearchName.Text;
                    var list = ctx.Categories.Where(c => c.CategoryName.Contains(sname));
                    listCategory = list.AsEnumerable();
                    LoadData();

                    if (listCategory.Count() == 0)
                    {
                        MessageBox.Show(this, "No items match your search!", "Search Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        //LoadData();
                    }
                }
            }
        }



        private void btnDelete_Click(object sender, EventArgs e)
        {
            string temp = btnDelete.Text.Equals("Disable") ? "Disable" : "Enable";
            DialogResult result = MessageBox.Show(this, "Do you want to " + temp + " " + category.CategoryName + " ?", "Change Status", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                int id = Convert.ToInt32(txtCategoryID.Text);
                if (btnDelete.Text.Equals("Disable"))
                {
                    if (ctx != null)
                    {
                        ctx.Disable_Category(id);
                        ctx.SubmitChanges();
                    }
                }
                else
                {
                    if (ctx != null)
                    {
                        ctx.Enable_Category(id);
                        ctx.SubmitChanges();
                    }
                }

                RefreshData();
            }
        }

        private void grvCategory_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

                DataGridViewRow row = grvCategory.Rows[e.RowIndex];
                int id = 0;
                id = Convert.ToInt32(row.Cells[0].Value.ToString());
                if (id > 0)
                {
                    category = ctx.Categories.Single(c => c.CategoryID == id);
                    txtCategoryID.Text = category.CategoryID.ToString();
                    txtCategoryName.Text = category.CategoryName;
                    txtDescription.Text = category.Description;

                    if (category.Status == true)
                    {
                        rbtnActive.SelectedIndex = 0;
                        btnDelete.Text = "Disable";
                        btnDelete.ImageIndex = 9;
                    }
                    else
                    {
                        rbtnActive.SelectedIndex = 1;
                        btnDelete.Text = "Enable";
                        btnDelete.ImageIndex = 10;
                    }
                    btnDelete.Enabled = true;
                    btnUpdate.Enabled = true;
                    btnAdd.Enabled = false;
                }
            }
            catch (Exception ex)
            {

            }
        }

       



       



    }
}